<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-subject-edit" th:object="${subject}">
        <input id="subjectId" name="subjectId" th:field="*{subjectId}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">类别：</label>
            <div class="col-sm-8">
                <select id="type" class="form-control m-b" th:with="type=${@dict.getType('zl_song_leibie')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"
                            th:field="*{type}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <input id="miaoshu" name="miaoshu" th:field="*{miaoshu}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">级别：</label>
            <div class="col-sm-8">
                <select id="level" name="level" class="form-control m-b"
                        th:with="type=${@dict.getType('zl_subject_level')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:data-remark="${dict.remark}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">价格：</label>
            <div class="col-sm-8">
                <input id="price" name="price" th:field="*{price}" class="form-control" type="text" disabled="disabled">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input id="remark" name="remark" th:field="*{remark}" class="form-control" type="text">
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script type="text/javascript">
    var options_year_class = eval("[[${@dict.getTypeJson('zl_subject_year')}]]".replace(/&quot;/g, "'"));
    var options_level_class = eval("[[${@dict.getTypeJson('zl_subject_level')}]]".replace(/&quot;/g, "'"));
    function reashClass() {
        var options;
        var _options_html = "";
        if ($("#type").val() == "level") {
            console.log("level")
            options = options_level_class;
        } else {
            console.log("year")
            options = options_year_class;
        }
        for (var i = 0; i < options.length; i++) {
            var option = options[i];
            console.log(option)
            _options_html = _options_html + "<option value='" + option.dictValue + "' data-remark='"+option.remark+"'>" +  option.dictLabel + "</option>"
        }
        $("#level").html(_options_html);
        $("#price").val($("#level option:selected").attr("data-remark"))
    }
    reashClass();
    $("#type").on("change", function () {
        console.log(1)
        reashClass();
    })
    $("#price").val($("#level option:selected").attr("data-remark"))
    $("#level").on("change", function () {
        $("#price").val($("#level option:selected").attr("data-remark"))
    })

    var prefix = ctx + "project/subject"
    $("#form-subject-edit").validate({
        rules: {
            xxxx: {
                required: true,
            },
        }
    });

    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }

    function edit() {

        var subjectId = $("input[name='subjectId']").val();
        var miaoshu = $("input[name='miaoshu']").val();
        var type = $("#type option:selected").val();
        var level = $("#level option:selected").val();
        var price = $("input[name='price']").val();
        var remark = $("input[name='remark']").val();


        $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "project/subject/edit",
            data: {
                "type": type,
                "subjectId": subjectId,
                "level": level,
                "miaoshu": miaoshu,
                "price": price,
                "remark": remark

            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.saveSuccess(data);
            }
        });
    }

</script>
</body>
</html>
